package com.example.teaching_server.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.List;

/**
 * 用户活动数据传输对象
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "用户活动信息")
public class UserActivityDTO {

    @Schema(description = "活动ID", example = "1")
    private Long activityId;

    @Schema(description = "活动名称", example = "数学作业打卡活动")
    private String title;

    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "开始日期", example = "2024-09-01")
    private LocalDate startDate;

    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "结束日期", example = "2024-09-30")
    private LocalDate endDate;

    @Schema(description = "活动总天数", example = "30")
    private Integer totalDays;

    @Schema(description = "已打卡天数", example = "15")
    private Integer checkedDays;

    @Schema(description = "是否打卡（至少打一次算已打卡）", example = "true")
    private boolean isChecked;

    @Schema(description = "打卡记录详情")
    private List<CheckinRecordDTO> records;

    /**
     * 获取剩余天数
     */
    public Integer getRemainingDays() {
        if (totalDays != null && checkedDays != null) {
            return Math.max(0, totalDays - checkedDays);
        }
        return 0;
    }

    /**
     * 计算完成率
     */
    public Double getCompletionRate() {
        if (totalDays != null && totalDays > 0) {
            return (checkedDays * 100.0) / totalDays;
        }
        return 0.0;
    }

    /**
     * 判断活动是否已结束
     */
    public boolean isEnded() {
        return endDate != null && endDate.isBefore(LocalDate.now());
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "打卡记录详情")
    public static class CheckinRecordDTO {

        @JsonFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "打卡日期", example = "2024-09-01")
        private LocalDate checkDate;

        @Schema(description = "打卡时间", example = "14:30:00")
        private String checkTime;

        @Schema(description = "描述", example = "完成了今天的数学作业")
        private String description;

        @Schema(description = "图片URL列表")
        private List<String> images;

        /**
         * 是否有图片
         */
        public boolean hasImages() {
            return images != null && !images.isEmpty();
        }
    }
}
